<script setup lang='ts'>
  import { nextTick, reactive, ref } from 'vue';
  import { MoreActionsApi, statisticsApi } from '@/api/controller';
  import { ToastSuccess, Toastloading } from '@/utils/public';
  import serachUserid from '@/components/serachUserid/index.vue';
  import xmUpload from '@/components/Upload/xm-upload.vue';
  const formLabelWidth = '120px'
  const dataForm = ref()
  const $emit = defineEmits(['updateList'])
  const action = reactive({
    dialogFormVisible:false,
    disabled:false,
    list:[] as Array<any>,
    typeArr:[] as Array<any>,
  })
  interface formQuert {
    id:number
    user_id:number
    date_st:number | string
    date_ed:number
    type:number
    excelUrl:string
    from_user_ids:string
    to_user_ids:string
    goods_id:string
    is_bag:number
  }
  let form = reactive({} as formQuert)
  const formRules = {
    type: [{ required: true, trigger: 'blur', message: '请选择查询类型' }],
    date_st: [{ required: true, trigger: 'blur', message: '请选择开始日期' }],
    date_ed: [{ required: true, trigger: 'blur', message: '请选择结束日期' }],
    excelUrl: [{ required: true, trigger: 'blur', message: '请上传Excel文件' }],
    goods_id: [{ required: true, trigger: 'blur', message: '请输入礼物id' }],
    is_bag: [{ required: true, trigger: 'blur', message: '请选择是否背包礼物' }],
  }
  const handleSubmit = ()=>{
    dataForm.value.validate((valid:any)=>{
      if(!valid) return
      const loading = Toastloading()
      let submitLoaidng = false
      MoreActionsApi.addDownListexcel(form).then(()=>{
        submitLoaidng = true
        loading.close()
        closeDialog()
        $emit('updateList')
        ToastSuccess('创建成功')
      }).catch(()=>{
        loading.close()
      })
    })
  }
  const openDialog = async(query:any = {})=> {
    const {
      typeArr=[]
    } = query
    action.typeArr = typeArr
    action.dialogFormVisible = true
  }
  const closeDialog = ()=>{
    form = reactive({} as formQuert)
    nextTick(() => {
      dataForm.value.clearValidate();
    });
    action.dialogFormVisible = false
  }
  defineExpose({
    openDialog
  })
</script>

<template>
  <div>
    <el-dialog
    top="5vh"
    class="xm-dialog input-width xm-dialog-cont-y"
    v-model="action.dialogFormVisible"
    :close-on-click-modal="action.disabled"
    :show-close="action.disabled"
    :close-on-press-escape="action.disabled"
    width="45%"
    title="创建查询数据">
      <el-form ref="dataForm" :model="form" :rules="formRules">
        <el-form-item :label-width="formLabelWidth" label="查询类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择查询类型">
            <el-option
              v-for="item in action.typeArr"
              :key="item.type"
              :label="item.text"
              :value="item.type" />
          </el-select>
        </el-form-item>
        <el-form-item v-if="[1,5].indexOf(form.type) > -1" :label-width="formLabelWidth" label="用户UUID">
          <serachUserid v-model:user_id="form.user_id" />
        </el-form-item>
        <template v-if="[1,3,4,5].indexOf(form.type) > -1">
          <el-form-item :label-width="formLabelWidth" label="开始日期" prop="date_st">
            <el-date-picker
              v-model="form.date_st"
              type="date"
              format="YYYY-MM-DD"
              value-format="YYYY-MM-DD"
              placeholder="请选择开始日期"
            />
          </el-form-item>
          <el-form-item :label-width="formLabelWidth" label="结束日期" prop="date_ed">
            <el-date-picker
              v-model="form.date_ed"
              type="date"
              format="YYYY-MM-DD"
              value-format="YYYY-MM-DD"
              placeholder="请选择结束日期"
            />
          </el-form-item>
        </template>
        <el-form-item v-if="form.type == 2" :label-width="formLabelWidth" label="Excel" prop="excelUrl">
          <xmUpload list-type="text" :limit="1" v-model:file-url="form.excelUrl" :data="{type:24}" />
        </el-form-item>
        <el-form-item
          v-if="form.type == 3"
          :label-width="formLabelWidth"
          label="是否背包礼物"
          prop="is_bag">
          <el-select v-model="form.is_bag" placeholder="请选择是否背包礼物">
            <el-option label="否" :value="1" />
            <el-option label="是" :value="2" />
          </el-select>
        </el-form-item>
        <template v-if="form.type == 4">
          <el-form-item :label-width="formLabelWidth" label="送礼人UUID">
            <el-input
              :disabled="action.disabled"
              v-model="form.from_user_ids"
              :autosize="{ minRows: 6, maxRows: 6}"
              type="textarea"
              placeholder="请输入送礼人UUID英文逗号分割例：888,999"
            />
          </el-form-item>
          <el-form-item :label-width="formLabelWidth" label="收礼人UUID">
            <el-input
              :disabled="action.disabled"
              v-model="form.to_user_ids"
              :autosize="{ minRows: 6, maxRows: 6}"
              type="textarea"
              placeholder="请输入收礼人UUID英文逗号分割例：888,999"
            />
          </el-form-item>
        </template>
        <el-form-item
          v-if="form.type == 4 || form.type == 3"
          :label-width="formLabelWidth"
          label="礼物id"
          prop="goods_id">
          <el-input
            :disabled="action.disabled"
            v-model="form.goods_id"
            :autosize="{ minRows: 6, maxRows: 6}"
            type="textarea"
            placeholder="请输入礼物id英文逗号分割例：888,999"
          />
        </el-form-item>
      </el-form>
      <template v-if="!action.disabled" #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="handleSubmit()">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>